package com.xkcun.third.aliyun.faceVerify;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.DescribeFaceVerifyRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeFaceVerifyResponse;
import com.aliyuncs.cloudauth.model.v20190307.InitFaceVerifyRequest;
import com.aliyuncs.cloudauth.model.v20190307.InitFaceVerifyResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.xkcun.third.aliyun.faceVerify.config.FaceVerifyConfigProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@AllArgsConstructor
public class FaceVerifyService {


    private  final  FaceVerifyConfigProperties faceVerifyConfigProperties;

     /**
      * 功能描述: <br>
      * 〈〉每次开始认证前通过本接口获取CertifyId，用来串联认证请求中的各个接口。
      * @Param: certName 身份证姓名
      *         certNo   身份证号码
      *         returnUrl 返回地址
      *         metaInfo 调用信息
      *
      * @Return: 
      * @Author: yanfaze
      * @Date: 2021/8/9 15:03
      */
    public  InitFaceVerifyResponse.ResultObject  initFaceVerify(Long sceneId,String certName,String certNo,String returnUrl,String metaInfo) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile(faceVerifyConfigProperties.getRegionId(), faceVerifyConfigProperties.getAccessKeyId(), faceVerifyConfigProperties.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        InitFaceVerifyRequest request = new InitFaceVerifyRequest();
        request.setSceneId(sceneId);
        String orderNo = System.currentTimeMillis()+"";
        request.setOuterOrderNo(orderNo);
        request.setProductCode("ID_PRO");
        request.setCertType("IDENTITY_CARD");
        request.setMetaInfo(metaInfo);
        request.setCertName(certName);
        request.setCertNo(certNo);
        request.setReturnUrl(returnUrl);

        try {
            InitFaceVerifyResponse response = client.getAcsResponse(request);
            return response.getResultObject();
        } catch (ServerException e) {
            e.printStackTrace();
            throw  new  Exception(e.getErrMsg());
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            log.error("ErrMsg:" + e.getErrMsg());
            log.error("RequestId:" + e.getRequestId());
            throw  new  Exception(e.getErrMsg());
        }
    }


    /**
     * 功能描述: <br>
     * 〈〉当接入方移动端收到回调之后，其服务端可以调用此接口，来获取相应的认证状态和认证资料。
     * @Param: SceneId 身份证姓名
     *         CertifyId   身份证号码
     * @Return:
     * @Author: yanfaze
     * @Date: 2021/8/9 15:03
     */
    public  DescribeFaceVerifyResponse.ResultObject  describeFaceVerify(Long sceneId,String certifyId) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile(faceVerifyConfigProperties.getRegionId(), faceVerifyConfigProperties.getAccessKeyId(), faceVerifyConfigProperties.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        DescribeFaceVerifyRequest request = new DescribeFaceVerifyRequest();
        request.setSceneId(sceneId);
        request.setCertifyId(certifyId);
        try {
            DescribeFaceVerifyResponse acsResponse = client.getAcsResponse(request);
            return acsResponse.getResultObject();
        } catch (ServerException e) {
            e.printStackTrace();
            throw  new  Exception(e.getErrMsg());
        } catch (ClientException e) {
            log.error("ErrCode:" + e.getErrCode());
            log.error("ErrMsg:" + e.getErrMsg());
            log.error("RequestId:" + e.getRequestId());
            throw  new  Exception(e.getErrMsg());
        }
    }

}
